﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yunchang.NetworkLayer;
/// <summary>
/// From：张帅
/// PS：自走棋结算界面
/// </summary> 
namespace UI
{
    public class AutoChessBillingInfoPage : OPPage
    {
        public AutoChessBillingInfoItem pref;
        public Transform parent;

        protected override void DoOpen()
        {
            base.DoOpen();
            //Register();
            Init();
        }

        protected override void DoClose(bool immediately)
        {
            base.DoClose(immediately);
            //UnRegister();
        }

        //private void Register()
        //{
        //    NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessGetReport, SCAutoChessGetReport);
        //}

        //private void UnRegister()
        //{
        //    NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessGetReport, SCAutoChessGetReport);
        //}

        //private void Init()
        //{
        //    NetworkDriver.Instance.SendSocketBlock(OpDefine.CSAutoChessGetReport, new CSAutoChessGetReport(), OpDefine.SCAutoChessGetReport);
        //}

        private void Init()
        {
            var msg = AutoChess.AutoChessManager.Ins.cacheData.overScoreData;
            UpdateList(msg.reportInfos);
        }

        //void SCAutoChessGetReport(object obj)
        //{
        //    var msg = obj as SCAutoChessGetReport;
        //    if(msg != null)
        //    {
        //        UpdateList(msg.infos);
        //    }
        //}

        void UpdateList(List<AutoChessPlayerReportInfo> list)
        {
            list.Sort((AutoChessPlayerReportInfo a, AutoChessPlayerReportInfo b) => {
                if(a.rank == 0 && b.rank != 0)
                {
                    return 1;
                }
                else if(a.rank != 0 && b.rank == 0)
                {
                    return -1;
                }
                else
                {
                    return a.rank - b.rank;
                }
            });
            pref.gameObject.SetActive(true);
            for (int i = 0; i < list.Count; i++)
            {
                var item = Instantiate(pref);
                item.transform.transform.SetParent(parent);
                item.transform.localScale = Vector3.one;
                item.SetItem(list[i]);
            }
            pref.gameObject.SetActive(false);
        }

        public void ClosePage()
        {
            Close();
            AutoChess.AutoChessManager.Ins.SendCSAutoChessPlayerQuit();
        }
    }
}
